package com.aopcinemas.visitor.ui;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.lang.acl.ACLMessage;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.behaviour.CampaignRequestInitiator;
import com.aopcinemas.visitor.ontology.CampaignRequest;
import com.aopcinemas.visitor.ontology.GetCampaign;
import com.aopcinemas.visitor.ontology.VisitorOntology;

public class CampaignRequestDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private JLabel movieName = new JLabel();
    private JLabel movieDescription = new JLabel();
    private JLabel movieTime = new JLabel();
    private JLabel moviePrice = new JLabel();
    private JLabel numberOfParticipant = new JLabel();
    private GetCampaign getCampaign;

    public CampaignRequestDialog(JFrame owner, final VisitorAgent agent) {
        super(owner);

        JPanel panel = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 3, 3, 3);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        panel.setLayout(layout);

        layout.setConstraints(movieName, constraints);
        panel.add(movieName);

        layout.setConstraints(movieDescription, constraints);
        panel.add(movieDescription);

        layout.setConstraints(movieTime, constraints);
        panel.add(movieTime);

        layout.setConstraints(moviePrice, constraints);
        panel.add(moviePrice);

        layout.setConstraints(numberOfParticipant, constraints);
        panel.add(numberOfParticipant);

        JButton requestButton = new JButton("Participate");
        requestButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {

                ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
                message.setLanguage(agent.getCodec().getName());
                message.setOntology(VisitorOntology.getInstance().getName());
                message.addReceiver(getCampaign.getOwner());

                CampaignRequest campaignRequest = new CampaignRequest();
                campaignRequest.setPerson(agent.getPerson());

                try {
                    agent.getContentManager().fillContent(message, new Action(getCampaign.getOwner(), campaignRequest));
                } catch (CodecException e) {
                    e.printStackTrace();
                } catch (OntologyException e) {
                    e.printStackTrace();
                }

                agent.addBehaviour(new CampaignRequestInitiator(agent, message));
            }
        });

        panel.add(requestButton);
        getContentPane().add(panel);
        setSize(300, 300);
    }

    public void reinit(GetCampaign getCampaign) {
        this.getCampaign = getCampaign;
        Schedule schedule = getCampaign.getCampaign().getSchedule();

        movieName.setText("Movie: " + schedule.getMovie().getName());
        movieDescription.setText("Description: " + schedule.getMovie().getDescription());
        movieTime.setText("Time: " + schedule.getDate());
        moviePrice.setText("Price: " + schedule.getBasePrice());
        numberOfParticipant.setText("Number of participants: " + getCampaign.getStartTime());

        repaint();

    }
}
